home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 13554 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.8 KB

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: accessing structures via pointers
  5. Date: Mon, 08 Apr 96 20:13:54 GMT
  6. Organization: none
  7. Message-ID: <828994434snz@genesis.demon.co.uk>
  8. References: <1996Apr8.144012.25767@leeds.ac.uk>
  9. Reply-To: fred@genesis.demon.co.uk
  10. X-NNTP-Posting-Host: genesis.demon.co.uk
  11. X-Newsreader: Demon Internet Simple News v1.27
  12. X-Mail2News-Path: genesis.demon.co.uk
  13.  
  14. In article <1996Apr8.144012.25767@leeds.ac.uk>
  15.            csyamc@scs.leeds.ac.uk "A M Casey" writes:
  16.  
  17. >Hi I'm calling the function getgrent(), which resturns a structure
  18. >as defined in grp.h, ie
  19. >
  20. > struct group {
  21. >                 char    *gr_name;   /* the name of the group */
  22. >                 char    *gr_passwd; /* the encrypted group password */
  23. >                 gid_t   gr_gid;     /* the numerical group ID */
  24. >                 char    **gr_mem;   /* vector of pointers to member names */
  25. >          };
  26. >the trouble is it returns a pointer to the structure:
  27. >
  28. >struct group *getgrent(void);
  29. >
  30. >
  31. >and I havent got a clue how to access it. I've lookied at the faq, but
  32. >I'm still stuck.
  33.  
  34. The FAQ doesn't generally cover basic language topics that are explained
  35. in any C language textbook.
  36.  
  37. >I need something like
  38. >
  39. >printf("the group name is %s\n",tempgroup.gr_name);
  40. >
  41. >but that doesnt work because tempgroup is a pointer.
  42. >
  43. >How do I do this?
  44.  
  45. If tempgroup is a pointer to a structure then *tempgroup is a structure
  46. and (*tempgroup).gr_name gives you the particular member. The parentheses
  47. are there because . has higher precedence than *. The language provides a
  48. shorter way of writing the same thing, that is tempgroup->gr_name.
  49.  
  50. -- 
  51. -----------------------------------------
  52. Lawrence Kirby | fred@genesis.demon.co.uk
  53. Wilts, England | 70734.126@compuserve.com
  54. -----------------------------------------
  55.